package cn.getech.data.development.service.impl;

import cn.getech.data.development.constant.StoreTableType;
import cn.getech.data.development.entity.JobNodeConf;
import cn.getech.data.development.entity.ProcTable;
import cn.getech.data.development.entity.TableInfo;
import cn.getech.data.development.mapper.ProcTableMapper;
import cn.getech.data.development.mapper.TableInfoMapper;
import cn.getech.data.development.service.ProcTableService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 项目数据表关系表 服务实现类
 * </p>
 *
 * @author zenith
 * @since 2019-07-08
 */
@Slf4j
@Service
public class ProcTableServiceImpl extends ServiceImpl<ProcTableMapper, ProcTable> implements ProcTableService {

    @Autowired
    private TableInfoMapper tableInfoMapper;
    @Override
    public boolean checkIfRelation(Integer procId, Integer tableId) {
        return this.baseMapper.selectCount(new QueryWrapper<ProcTable>().eq("proc_id",procId).eq("table_id",tableId))>0;
    }

    @Override
    public boolean checkTaskRelation(Integer proc_id,Integer table_id,  Integer tableType) {
        // 判断实时任务使用引用当前表
        List<String> taskList = baseMapper.selectRealTaskList(proc_id, tableType);
        if (CollectionUtils.isNotEmpty(taskList)) {
            for (String config : taskList) {
                if (StringUtils.isNotEmpty(config)) {
                    try {
                        JSONObject json = JSONObject.parseObject(config);
                        Integer targetTableId = json.getInteger("targetTableId");
                        Integer tableId = json.getInteger("tableId");
                        if (table_id.equals(targetTableId)) {
                            return false;
                        }
                        if (table_id.equals(tableId)) {
                            return false;
                        }
                    } catch (Exception ignored) {
                    }
                }
            }
        }
        if (StoreTableType.OFFLINE.getCode().equals(tableType)) {
            List<Map<String, String>> list = baseMapper.findOffTaskRelation(proc_id, table_id);
            TableInfo table = tableInfoMapper.getTableById(table_id);
            for (Map<String, String> map : list) {
                if (map != null && table != null ) {
                    if (map.get("tableName") != null && map.get("dbName") != null ) {
                        if (map.get("tableName").equals(table.getTableName())
                                &&  map.get("dbName").equals(table.getDbName())) {
                            return false;
                        }
                    }
                    if (map.get("tableIds") != null){
                        try {
                            for (Object tableId : JSONArray.parseArray(map.get("tableIds"))) {
                                if (tableId != null) {
                                    if (table_id.equals(Integer.parseInt(tableId.toString()))) {
                                        return false;
                                    }
                                }
                            }
                        } catch (Exception e) {
                            log.warn("解析json错误,tableIds值为:{}",map.get("tableIds"));
                        }
                    }
                }
            }

        }
        return true;
    }
}
